﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace filesPartialRenamer
{
    public partial class Form1 : Form
    {
        public List<string> files = new List<string>();
        public bool odd = true;//nieparzystość
        public int begNr = 0;
        public decimal serNr = 0;
        public string end = "KONIEC";
        public string tn = "Ta nazwa:";
        public string it = "I to rozszerzenie pliku:";
        public string dn = "Dodaj nazwę pliku:";
        public string ztr = "Zmień tylko rozszerzenia plików na:";
        public string ztn = "Zmień tylko nazwy plików na:";
        public string dr = "Dodaj rozszerzenie pliku:";

        public Form1()
        {
            InitializeComponent();
            cBAN();
            cBCC(0);
        }

        public void addTxt2Box(string txt)
        {
            string temp = txt + "\n" + richTextBox1.Text.Split('\n')[richTextBox1.Text.Split('\n').Length - 1];
            if (richTextBox1.Text != "")
                richTextBox1.Text = richTextBox1.Text.Substring(0, richTextBox1.Text.LastIndexOf("\n")) + temp;
            else
                richTextBox1.Text = temp;
            richTextBox1.Update();
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.ScrollToCaret();
        }

        public void appTxt2Box(string txt)
        {
            if (richTextBox1.InvokeRequired)
                richTextBox1.Invoke(new Action(() => { addTxt2Box(txt); }));
            else
                addTxt2Box(txt);
        }

        public List<string> rewind(List<string> pliki)
        {
            List<string> temp = new List<string>();

            for (int i = pliki.Count - 1; i >= 0; i--)
                temp.Add(pliki[i]);

            return temp;
        }

        private void checkBoxPar_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxPar.Checked == true)
            {
                odd = false;
                checkBoxNPar.Checked = false;
            }
            else
            {
                odd = true;
                checkBoxNPar.Checked = true;
            }
        }

        private void checkBoxNPar_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxNPar.Checked == true)
            {
                odd = true;
                checkBoxPar.Checked = false;
            }
            else
            {
                odd = false;
                checkBoxPar.Checked = true;
            }
        }

        public bool filesChoose()
        {
            bool res = false;
            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                string[] temp = openFileDialog1.FileNames;
                files.Clear();
                for (int i = 0; i < temp.Length; i++)
                {
                    files.Add(temp[i]);
                }
                res = true;
            }
            return res;
        }

        public string getNextName(string mask, string ext, string oldPth, string newPth)
        {
            string name = createFName(mask, ext, oldPth);
            string o = newPth + name;
            while (checkBoxNotReplace.Checked && File.Exists(o))
            {
                if (checkBoxDecreasing.Checked == true)
                    if (checkBox1AA.Checked)
                        serNr--;
                    else
                        serNr -= 2;
                else
                    if (checkBox1AA.Checked)
                        serNr++;
                    else
                        serNr += 2;
                name = createFName(mask, ext, oldPth);
                o = newPth + name;
            }
            return name;
        }

        public string createFName(string mask, string ext, string oldPth)
        {
            string name = "";
            if (checkBoxName.Checked && checkBoxExt.Checked)
            {
                if (checkBoxAddNr.Checked)
                    name = mask.Substring(0, mask.LastIndexOf('.')) + serNr + "." + mask.Substring(mask.LastIndexOf('.') + 1);
                else
                    name = mask;
            }
            else if (checkBoxName.Checked && !checkBoxExt.Checked)
            {
                if (checkBoxAddNr.Checked)
                    name = mask + serNr + ext;
                else
                    name = mask + ext;
            }
            else if (!checkBoxName.Checked && checkBoxExt.Checked)
            {
                if (checkBoxAddNr.Checked)
                    name = Path.GetFileNameWithoutExtension(oldPth) + serNr + "." + mask;
                else
                    name = Path.GetFileNameWithoutExtension(oldPth) + "." + mask;
            }
            return name;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (filesChoose())
            {
                string ext = "";
                string oldPth = "";
                string newPth = "";
                string name = "";
                serNr = numericUpDown1.Value;
                string mask = textBoxMaska.Text;

                if (!checkBox1AA.Checked)
                {
                    if (odd && serNr % 2 == 0)
                        serNr++;
                    if (!odd && serNr % 2 == 1)
                        serNr++;
                }
                if (checkBoxReverse.Checked)
                    files = rewind(files);


                for (int j = 0; j < files.Count; j++)
                {
                    string plik = files[j];

                    newPth = Path.GetDirectoryName(plik) + "\\";
                    oldPth = plik;
                    appTxt2Box(oldPth + "\n");
                    ext = Path.GetExtension(plik);

                    if (!checkBoxAddNr.Checked)
                        name = createFName(mask, ext, oldPth);
                    else
                        name = getNextName(mask, ext, oldPth, newPth);

                    string o = newPth + name;
                    if (!File.Exists(o) || !checkBoxNotReplace.Checked)
                    {
                        if (File.Exists(o))
                            File.Delete(o);
                        File.Move(oldPth, o);
                    }
                    if (checkBoxDecreasing.Checked == true)
                        if (checkBox1AA.Checked)
                            serNr--;
                        else
                            serNr -= 2;
                    else
                        if (checkBox1AA.Checked)
                            serNr++;
                        else
                            serNr += 2;

                    appTxt2Box(o + "\n");
                    appTxt2Box("\n");
                }
                appTxt2Box("KONIEC\n");
                files.Clear();
            }
        }

        private void buttonLoadTxt_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                string[] temp = openFileDialog1.FileNames;
                files.Clear();
                string oldPth = "";
                string newPth = "";
                for (int i = 0; i < temp.Length; i++)
                {
                    string[] lines = File.ReadAllLines(temp[i]);

                    for (int j = 0; j < lines.Length; j++)
                    {
                        while (!lines[j].Equals(""))
                        {
                            newPth = lines[j];
                            j++;
                            oldPth = lines[j];
                            j++;
                        }
                        File.Move(oldPth, newPth);
                    }
                }
            }
        }

        private void checkBox1AA_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1AA.Checked)
            {
                checkBoxNPar.Enabled = false;
                checkBoxPar.Enabled = false;
            }
            else
            {
                checkBoxNPar.Enabled = true;
                checkBoxPar.Enabled = true;
            }
        }

        private void checkBoxName_CheckedChanged(object sender, EventArgs e)
        {
            cBCC(1);
        }

        public void cBCC(int sender)
        {
            if (checkBoxName.Checked && checkBoxExt.Checked)
            {
                checkBoxName.Text = tn;
                checkBoxExt.Text = it;
            }
            else if (!checkBoxName.Checked && checkBoxExt.Checked)
            {
                checkBoxName.Text = dn;
                checkBoxExt.Text = ztr;
                checkBoxExt.Checked = true;
            }
            else if (!checkBoxExt.Checked && checkBoxName.Checked)
            {
                checkBoxName.Text = ztn;
                checkBoxExt.Text = dr;
                checkBoxName.Checked = true;
            }
            else if (!checkBoxExt.Checked && !checkBoxName.Checked)
            {
                if (sender == 1)
                {
                    checkBoxName.Text = dn;
                    checkBoxExt.Text = ztr;
                    checkBoxExt.Checked = true;
                }
                else if (sender == 2)
                {
                    checkBoxName.Text = ztn;
                    checkBoxExt.Text = dr;
                    checkBoxName.Checked = true;
                }
            }
        }

        private void checkBoxExt_CheckedChanged(object sender, EventArgs e)
        {
            cBCC(2);
        }

        private void checkBoxAddNr_CheckedChanged(object sender, EventArgs e)
        {
            cBAN();
        }

        public void cBAN()
        {
            if (!checkBoxAddNr.Checked)
            {
                label1.Enabled = false;
                numericUpDown1.Enabled = false;
                checkBoxDecreasing.Enabled = false;
                checkBox1AA.Enabled = false;
                checkBoxNPar.Enabled = false;
                checkBoxPar.Enabled = false;
            }
            else
            {
                label1.Enabled = true;
                numericUpDown1.Enabled = true;
                checkBoxDecreasing.Enabled = true;
                checkBox1AA.Enabled = true;
                checkBoxNPar.Enabled = true;
                checkBoxPar.Enabled = true;
            }
        }

        private void polskiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            end = "KONIEC";
            checkBoxNotReplace.Text = "Nie zastępuj istniejących plików\ni ewentualnie dopasuj następny nr";
            checkBoxDecreasing.Text = "Malejąco";
            checkBoxReverse.Text = "Odwrotna kolejność plików";
            buttonLoadTxt.Text = "Wczytaj ścieżki z txt";
            label1.Text = "Od nr:";
            checkBoxNPar.Text = "Nieparzyste";
            checkBoxPar.Text = "Parzyste";
            checkBox1AA.Text = "Po kolei";
            checkBoxAddNr.Text = "Dodaj nr porządkowy";
            checkBoxExt.Text = "Zmień tylko rozszerzenia plików na:";
            checkBoxName.Text = "Zmień tylko nazwy plików na:";
            buttonUndo.Text = "Cofnij";
            buttonStart.Text = "START";
            Text = "Zmieniacz nazw plików";
            tn = "Ta nazwa:";
            it = "I to rozszerzenie pliku:";
            dn = "Dodaj nazwę pliku:";
            ztr = "Zmień tylko rozszerzenia plików na:";
            ztn = "Zmień tylko nazwy plików na:";
            dr = "Dodaj rozszerzenie pliku:";
        }

        private void englishToolStripMenuItem_Click(object sender, EventArgs e)
        {
            end = "END";
            checkBoxNotReplace.Text = "Don't replace existing files\nbut eventually fit next nr";
            checkBoxDecreasing.Text = "Decreasing";
            checkBoxReverse.Text = "Reversed order of files";
            buttonLoadTxt.Text = "Read paths from txt";
            label1.Text = "From nr:";
            checkBoxNPar.Text = "Odd";
            checkBoxPar.Text = "Even";
            checkBox1AA.Text = "One after another";
            checkBoxAddNr.Text = "Add ordering number";
            checkBoxExt.Text = "Change only extensions to:";
            checkBoxName.Text = "Change only filenames to:";
            buttonUndo.Text = "Undo";
            buttonStart.Text = "START";
            Text = "Filenames changer";
            tn = "This name:";
            it = "And this extension:";
            dn = "Add filename:";
            ztr = "Change only extensions to:";
            ztn = "Change only filenames to:";
            dr = "Add extension:";
        }

        private void buttonUndo_Click(object sender, EventArgs e)
        {
            List<string> temp = richTextBox1.Text.Split('\n').ToList();
            string oldPth = "";
            string newPth = "";
            int i = temp.Count - 1;
            while (i > -1 && temp[i] != end)
                i--;
            i--;
            while (i > -1 && temp[i] != end)
            {
                if (temp[i] != "")
                {
                    oldPth = temp[i];
                    i--;
                    newPth = temp[i];
                    i--;
                    File.Move(oldPth, newPth);
                }
                if (i > -1 && temp[i] != end)
                    i--;
            }
            richTextBox1.Clear();
            for (int j = 0; j <= i; j++)
                addTxt2Box(temp[j] + "\n");
        }
    }

}

